package top.youjizi.middleware.dynamic.thread.pool.sdk.trigger.job;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import top.youjizi.middleware.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import top.youjizi.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import top.youjizi.middleware.dynamic.thread.pool.sdk.registry.IRegistry;

import java.util.List;

/**
 * @description: 线程池数据上报任务
 * @author: libai
 * @date: 2024/5/17
 */
public class ThreadPoolDataReportJob {
    
    
    private final Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);
    
    private final IDynamicThreadPoolService dynamicThreadPoolService;
    
    private final IRegistry registry;
    
    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }
    
    @Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList() {
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);
        logger.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));
        
        threadPoolConfigEntities.forEach(threadPoolConfigEntity -> {
            
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
            logger.info("动态线程池，上报线程池参数信息：{}", JSON.toJSONString(threadPoolConfigEntity));
            
        });
    }
    
    
}
